//
//  MBSpaceAttentionsCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/19.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceAttentionsCell.h"

@interface MBSpaceAttentionsCell () {
    UILabel                 *_numLb;
    UIImageView             *_headImgView;
    UILabel                 *_babyNameLb;
    UIButton                *_attentionBt;
    UIView                  *_lineView;
}

@end
@implementation MBSpaceAttentionsCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    //序号
    _numLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _numLb.textAlignment = NSTextAlignmentCenter;
    _numLb.textColor = UIColorFromHex(0x333333);
    _numLb.font = [UIFont systemFontOfSize:14.f];
    [self.contentView addSubview:_numLb];
    
    //头像
    _headImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _headImgView.backgroundColor = [UIColor clearColor];
    _headImgView.layer.cornerRadius = 80*PERCEN / 2.;
    _headImgView.layer.masksToBounds = YES;
    [self.contentView addSubview:_headImgView];
    
    //名字
    _babyNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _babyNameLb.textAlignment = NSTextAlignmentLeft;
    _babyNameLb.textColor = UIColorFromHex(0x333333);
    _babyNameLb.font = [UIFont systemFontOfSize:14.f];
    [self.contentView addSubview:_babyNameLb];
    
    //关注按钮
    _attentionBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _attentionBt.backgroundColor = [UIColor clearColor];
    [_attentionBt setImage:[UIImage imageNamed:@"jiaguanzhu_fs"] forState:UIControlStateNormal];
    [_attentionBt setImage:[UIImage imageNamed:@"yiguanzhu_fs"] forState:UIControlStateSelected];
    
    [_attentionBt addTarget:self action:@selector(onSelectAttentionAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_attentionBt];
    
    //分割线
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = UIColorFromHex(0xcccccc);
    [self.contentView addSubview:_lineView];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)onSelectAttentionAction:(UIButton *)bt
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForAttentionWithValue:withView:withDate:)]) {
        [_delegate onSelectItemForAttentionWithValue:@(bt.selected) withView:self withDate:_dataDic];
    }
}

- (void)layoutData
{
    _attentionBt.selected = !_attentionBt.selected;
}

//是否隐藏关注按钮
- (void)setIsHideForAttentionBt:(BOOL)isHideForAttentionBt
{
    _isHideForAttentionBt = isHideForAttentionBt;
    [self setNeedsLayout];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setSelectIndex:(NSInteger)selectIndex
{
    _selectIndex = selectIndex;
    [self setNeedsLayout];
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self setNeedsLayout];
}

- (void)setAttention:(NSString *)attention
{
    _attention = attention;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //value
    if (_dataDic) {
        _numLb.text = [NSString stringWithFormat:@"%d",(int)_selectIndex];
        [_headImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,[_dataDic objectForKey:@"FaceImg"]]] placeholderImage:[UIImage imageNamed:@"touxiang_attention"]];
        _babyNameLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"NickName"]];
        _attentionBt.hidden = _isHideForAttentionBt;
    }
    if ([_dataDic.allKeys containsObject:@"CareStatus"]) {
        if ([[_dataDic objectForKey:@"CareStatus"]intValue] == 0) {
            _attentionBt.selected = NO;
        } else {
            _attentionBt.selected = YES;
        }
    } else {
        _attentionBt.selected = YES;
    }
//    _attentionBt.selected = [_attention isEqualToString:@"1"] ? YES : NO;
    
    //frame
    float numWidth = [_numLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_numLb.font} context:NULL].size.width+6;
    _numLb.frame = CGRectMake((ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (self.height-20)/2., numWidth, 20);
    
    _headImgView.frame = CGRectMake(_numLb.right+20*PERCEN, (self.height-80*PERCEN)/2., 80*PERCEN, 80*PERCEN);
    
    _babyNameLb.frame = CGRectMake(_headImgView.right+15*PERCEN, _numLb.top, 160, 20);
    
    _attentionBt.frame = CGRectMake(self.width-160*PERCEN-(ISIPAD?440*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), (self.height-60*PERCEN)/2., 160*PERCEN, 60*PERCEN);
    
    _lineView.frame = CGRectMake((ISIPAD?440*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):0), self.height-1, (ISIPAD?SCREEN_WIDTH-880*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):SCREEN_WIDTH), 1);
}

@end
